import api from '../assets/js/api.js'
import com from '../assets/js/com.js'
import config from '../assets/js/config.js'
import utils from '../assets/js/utils.js'
const MyMixin = {
	data() {
		return {
			myIsLoading:true,
			myScrollTop:0,//页面滚动全局参数
			myRnd:0,//页面全局随机数
			myCurTab:0,//全局tabIndex,懒得注册
		}
	},
	onPageScroll(e) {
		this.myScrollTop = e.scrollTop
	},
	methods: {
		navto (e) {
			this.log(e)
			let data = {}
			let url = e.currentTarget.dataset.url
		    if(e !== undefined){
		    	data = e.currentTarget.dataset
		    }
		    let p = "?"
		    for (var i in data){
				if( i === 'eventOpts')continue;
				if( i === 'eventParams')continue;
				if( i === 'url')continue;
				p = p + (p === "?"?'':'&') + i + '=' + data[i]
		    }
		    this.log(e)
		    uni.navigateTo({
		        url: url + p
		    });
		},
		goto (url,e) {
			this.$win.goto(url,e)
		},
		gointo (url,data) {
		   this.$win.gointo(url,data)
		},
		goback (delta) {
			if(delta == undefined)delta = 1
		    uni.navigateBack({
		        delta: delta
		    });
		},
		gohome () {
			uni.switchTab({
			    url: '/pages/machine/index'
			});
		},
		
		isbool(param) {
			// this.log(param)
			if (param === '') {
				return false
			}
			if (param === []) {
				return false
			}
			if (param === "[]") {
				return false
			}
			if (param === 'undefined') {
				return false
			}
			if (param === undefined) {
				return false
			}
			if (param === {}) {
				return false
			}
			if (param === 0) {
				return false
			}
			if (param === "0") {
				return false
			}
			if (param === "null") {
				return false
			}
			if (param === null) {
				return false
			}
			if (param === false) {
				return false
			}
			return true
		},
		isnull(param) {
			// this.log(param)
			if (param == '') {
				return true
			}
			if (param == 'undefined') {
				return true
			}
			if (param == undefined) {
				return true
			}
			if (param == "null") {
				return true
			}
			if (param == null) {
				return true
			}
			if (param == []) {
				return true
			}
			if (param == "[]") {
				return true
			}
			return false
		},
		objval(obj,path){
			try{
				
				let v = obj
				if (path == undefined) path = []
				for(var i in path){
					v = v[path[i]]
				} 
				return v
			}catch(e){
				return ''
			}
		},
		objectvalue(obj,path){this.objval(obj,path)},
		objvalue(obj,path){this.objval(obj,path)},
		objectval(obj,path){this.objval(obj,path)},
		
		pic(picUrl){
			if(this.isnull(picUrl)){
				return config.picDefault
			}
			return api.getAssetBaseUrl() + picUrl
		},
		qr(code){
			if(this.isnull(code)){
				return config.picDefault
			}
			return api.getBaseUrl()+"/Qr?code=" + code
		},
		pics(picsUrl){
			if(picsUrl == undefined) return []
			let arr = [];
			picsUrl.map((data)=>{
				arr.push(this.pic(data))
			})
			return arr
		},
		picBg(picUrl) {
			var rnd = Math.floor(Math.random() * 10); //0-9
			var color = ["#ffd6d6", "#ffead6", "#fffed6", "#eeffd6", "#ddffd6", "#d6ffef", "#d6f1ff", "#dbd6ff", "#f8d6ff",
				"#ffd6e3"
			]
			if (this.isnull(pic)) {
				return "background-size: cover;background-color: " + color[rnd] + ";background-image: url('" + this.pic(picUrl) +
					"');";
			} else {
				return "background-size: cover;background-color: " + color[rnd] + ";";
			}
		},
		rndbg() {
			var rnd = Math.floor(Math.random() * 10); //0-9
			var color = ["#ffd6d6", "#ffead6", "#fffed6", "#eeffd6", "#ddffd6", "#d6ffef", "#d6f1ff", "#dbd6ff", "#f8d6ff",
				"#ffd6e3"
			]
			return "background-size: cover;background-color: " + color[rnd] + ";";
		},
		
		
		str2time(str) {
			if (!this.isbool(str)) return '-'
			var date = new Date(str);
			return this.getDateTime(date)
		},
		datetime(str) {return this.str2time(str) },
		
		getDate(date) {
			if (date == undefined) {
				date = new Date()
			}
			return date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) +
				"-" + date.getDate();
		},
		
		getTime(date) {
			if (date == undefined) {
				date = new Date()
			}
			return (date.getHours()<10?'0'+ date.getHours(): date.getHours()) +
				":" + (date.getMinutes()<10?'0'+ date.getMinutes(): date.getMinutes()) +
				":" +  (date.getSeconds()<10?'0'+ date.getSeconds(): date.getSeconds()) ;
		},
		getTimestamp(date) {
			return parseInt(this.getTimestampMs(date) / 1000);
		},
		getTimestampMs(date) {
			if (date == undefined) {
				date = new Date()
			}
			return date.getTime();
		},
		
		getDateTime(date) {
			if (date === undefined) {
				date = new Date()
			}
			return date.getFullYear() + "-" +
				((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) +
				"-" + date.getDate() +
				" " + (date.getHours()<10?'0'+ date.getHours(): date.getHours())  +
				":" + (date.getMinutes()<10?'0'+ date.getMinutes(): date.getMinutes()) +
				":" + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
		},
		
		timeLeft(str) {
			var date = new Date();
			var now = date.getTime();
			//设置截止时间
			//     var str = "2019/10/1 00:00:00";
			var endDate = new Date(str);
			var end = endDate.getTime();
			//时间差
			var leftTime = end - now;
			//定义变量 d,h,m,s保存倒计时的时间
			var d, h, m, s;
			d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
			h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
			m = Math.floor(leftTime / 1000 / 60 % 60);
			s = Math.floor(leftTime / 1000 % 60);
			if (leftTime > 1) {
				return d + "天" +
					h + "时" +
					m + "分";
				// + s + "秒";
			} else {
				//倒计时结束
				return "已过期";
			}
		},
		
		timePass(str) {
			var date = new Date();
			var now = date.getTime();
			//设置截止时间
			//     var str = "2019/10/1 00:00:00";
			var endDate = new Date(str);
			var end = endDate.getTime();
			//时间差
			//     var leftTime = end - now;
			var leftTime = now - end;
			//定义变量 d,h,m,s保存倒计时的时间
			var d, h, m, s;
			d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
			h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
			m = Math.floor(leftTime / 1000 / 60 % 60);
			s = Math.floor(leftTime / 1000 % 60);
		
			var showStr = "";
			// if (s > 0) { showStr = s + "秒" + showStr; }
			// if (m > 0) { showStr = m + "分" + showStr; }
			// if (h > 0) { showStr = h + "时" + showStr; }
			// if (d > 0) { showStr = d + "天" + showStr; }
			// return  showStr;
			if (s > 0) {
				showStr = s + "秒";
			}
			if (m > 0) {
				showStr = m + "分钟";
			}
			if (h > 0) {
				showStr = h + "小时";
			}
			if (d > 0) {
				showStr = d + "天";
			}
			return showStr;
		},
		showLoading(){
			this.log('trigger showLoading')
			this.myIsLoading = true
		},
		clearLoading(){
			this.log('trigger clearLoading')
			this.myIsLoading = false
		},
		call(phone){
			if(!this.isbool(phone)){
				this.$com.showToast('手机号错误')
				return
			}
			
			uni.makePhoneCall({
			    phoneNumber: phone //仅为示例
			});
		},
		viewPic(pic,pics){
			if(pics == undefined)pics = [pic]
			uni.previewImage({
				current: pic,
				urls: pics,
				longPressActions: {
					itemList: ['保存图片'],
					success: function(data) {
						this.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
					},
					fail: function(err) {
						this.log(err.errMsg);
					}
				}
			});
		},
		copy(text,message){
			if(message == undefined) message = '复制成功'
			uni.setClipboardData({
			    data: text,
			    success: ()=>{
			        this.$com.showToast(message)
			    }
			});
		},
		setClip(text,message){
			if(message == undefined) message = '复制成功'
			uni.setClipboardData({
			    data: text,
			    success: ()=>{
			        this.$com.showToast(message)
			    }
			});
		},
		//与sslide组件结合的方法
		mySlide(indexOrRefName,refName){
			if(refName === undefined) refName="mySlideBox"
			if(this.isbool(this.objval(this.$refs,[refName,indexOrRefName]))){
				if(this.$refs[refName][indexOrRefName].showStatus === false){
					this.$refs[refName][indexOrRefName].showSlide()
					this.$refs[refName].map((d,i)=>{
						if(this.isbool(d.hideSlide) && i != indexOrRefName && d.showStatus == true){
							 d.hideSlide()	
						}
					})
				}else{
					this.$refs[refName][indexOrRefName].hideSlide()	
				}
			}else{
				console.log(this.$refs[indexOrRefName])
				if(!utils.isNumber(indexOrRefName)){
					if(this.$refs[indexOrRefName].showStatus === false){
						this.$refs[indexOrRefName].showSlide()
					}else{
						this.$refs[indexOrRefName].hideSlide()	
					}
				}
			}
		},
		iriId(iri){
			return iri.split("/").pop()
		}
	}
}

export default MyMixin;
